package com.example.nuc_ms_beta.ui.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.webkit.WebView;
import android.widget.Button;
import android.widget.Toast;

import com.example.nuc_ms_beta.R;
import com.example.nuc_ms_beta.app.MyApplication;
import com.example.nuc_ms_beta.bean.activity.Collection;
import com.example.nuc_ms_beta.util.SharePreferencesUtil;
import com.example.nuc_ms_beta.util.UrlShareUtil;

import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.SaveListener;

import static com.example.nuc_ms_beta.R.id.activity_urlshow_good_bt;

/**
 * Created by bnhc on 17-5-23.
 */

public class UrlActionActivity extends Activity implements View.OnClickListener {

    private WebView urlshowactivity_wb;
    private String url;
    private String objectId;
    private String userObjectId;

    private Button activity_urlshow_comment, activity_urlshow_collect_bt, activity_urlshow_share_bt;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_urlshow);
        initView();
    }

    private void initView() {
        url = getIntent().getStringExtra("url");
        objectId = getIntent().getStringExtra("objectId");
        urlshowactivity_wb = (WebView) findViewById(R.id.urlshowactivity_wb);
        urlshowactivity_wb.loadUrl(url);
        activity_urlshow_comment = (Button) findViewById(R.id.activity_urlshow_coment_bt);
        activity_urlshow_comment.setOnClickListener(this);
        activity_urlshow_collect_bt = (Button) findViewById(R.id.activity_urlshow_collect_bt);
        activity_urlshow_collect_bt.setOnClickListener(this);
        activity_urlshow_share_bt = (Button) findViewById(R.id.activity_urlshow_share_bt);
        activity_urlshow_share_bt.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case activity_urlshow_good_bt:
                break;
            case R.id.activity_urlshow_collect_bt:
                doCollection();
                break;
            case R.id.activity_urlshow_coment_bt:
                Intent commentAcitivityIntent = new Intent(UrlActionActivity.this, CommentActivity.class);
                commentAcitivityIntent.putExtra("newsobjectId", objectId);
                startActivityForResult(commentAcitivityIntent, 1);
                break;
            case R.id.activity_urlshow_share_bt:
                UrlShareUtil.showShare(url);
                break;
        }
    }

    /**
     * Do Collection
     * <p>
     * String UserObject
     * String NewsObject
     */
    private void doCollection() {
        Boolean isLogin = (Boolean) SharePreferencesUtil.getData(MyApplication.context, "isLogin", true);
        if (!isLogin) {
            userObjectId = (String) SharePreferencesUtil.getData(MyApplication.context, "userObjectId", "0000");
            Collection collection = new Collection();
            collection.setUserId(userObjectId);
            collection.setNewsId(objectId);
            collection.save(new SaveListener<String>() {
                @Override
                public void done(String s, BmobException e) {
                    Toast.makeText(UrlActionActivity.this, "收藏成功!", Toast.LENGTH_SHORT).show();
                }
            });
        } else {
            Toast.makeText(UrlActionActivity.this, "用户未登录！", Toast.LENGTH_SHORT).show();
        }
    }


}
